package com.yanqu.road.server.cmd.activity.fivehero;

import com.yanqu.road.entity.activity.fivehero.FiveHeroConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FiveHeroProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.fivehero.FiveHeroModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_FIVEHERO_NOTIFY_EVENT, desc = "")
public class CrossFiveHeroNotifyEventCmd extends AbstractCommand {

    private FiveHeroProto.FiveHeroCitySearchRespMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = FiveHeroProto.FiveHeroCitySearchRespMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        FiveHeroConfig config = FiveHeroMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!StringUtils.isNullOrEmpty(reqMsg.getOtherResult().getReward())){
            player.getModule(CurrencyModule.class).addCurrency(reqMsg.getOtherResult().getReward(), eLogMoneyType.FiveHero,eLogMoneyType.FiveHeroEventReward);
        }
        if(reqMsg.getLuoyangResult().getCost() != 0){
            String reward = config.getLuoYangAttItem() + "=" + reqMsg.getLuoyangResult().getCost();
            player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(reward), eLogMoneyType.FiveHero,eLogMoneyType.FiveHeroAttackLuoYangCost);
        }
        FiveHeroProto.FiveHeroCitySearchRespMsg.Builder respMsg = reqMsg.toBuilder();
        if(!StringUtils.isNullOrEmpty(respMsg.getLuoyangResult().getNickName())){
            FiveHeroProto.FiveHeroCitySearchLuoYangResultMsg.Builder luoyangMsg = respMsg.getLuoyangResult().toBuilder();
            String language = player.getUserInfo().getLanguage();
            luoyangMsg.setNickName(ServerLanguageMgr.getContent(luoyangMsg.getNickName(), language));
            respMsg.setLuoyangResult(luoyangMsg);
        }
        player.sendPacket(ClientProtocol.U_FIVEHERO_CITY_SEARCH,respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
    }
}
